package com.wangjiang.controller;

import com.wangjiang.exception.ExceptionEnum;
import com.wangjiang.exception.NoFoundException;
import com.wangjiang.model.LogModel;
import com.wangjiang.rest.ResultGenerator;
import com.wangjiang.rest.entity.Result;
import com.wangjiang.service.LogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/log")
public class LogController {
    @Autowired
    private LogService logService;
    /**
     * 引入日志，注意都是"org.slf4j"包下
     */
    private final static Logger logger = LoggerFactory.getLogger(LogController.class);

    private final static String PRINT_PERMESSION = "log:read";

    @GetMapping("/{logId}")
    @RequiresRoles("admin")  // 这里的role  就是在MyShiroRealm里 doGetAuthorizationInfo 这边获取的用户权限。
    @RequiresPermissions(PRINT_PERMESSION)
//    @RequiresPermissions({"sys:role:save", "sys:role:update"})  // 多个权限这么写
    public Result findLogById (
            @PathVariable Integer logId
    ) throws Exception {
        logger.info("访问/log/{logId}接口，logId="+logId);
        LogModel log = logService.findLogById(logId);
        if(log == null ) {

            logger.error("根据log表异常：e=" + ExceptionEnum.ERROR_NOFOUND.getValue());
            throw new NoFoundException(ExceptionEnum.ERROR_NOFOUND.getValue());
        }
        return ResultGenerator.genSuccessResult(log);
    }
}
